package cn.worldyao.demo.service.impl;

import cn.worldyao.demo.entity.SysPermissionRole;
import cn.worldyao.demo.entity.SysPermissionRoleItem;
import cn.worldyao.demo.entity.SysRole;
import cn.worldyao.demo.mapper.SysPermissionRoleMapper;
import cn.worldyao.demo.service.SysPermissionRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yaozi
 * @since 2020-03-11
 */
@Service
public class SysPermissionRoleServiceImpl extends ServiceImpl<SysPermissionRoleMapper, SysPermissionRole> implements SysPermissionRoleService {


    @Autowired
    public SqlSessionFactory factory;


    @Override
    public boolean saveBatch(SysPermissionRoleItem sysPermissionRoleItem) {
        Set<String> newPremiss = new HashSet<>(Arrays.asList(sysPermissionRoleItem.getPermissions()));
        QueryWrapper<SysPermissionRole> sysPermissionRoleQueryWrapper = new QueryWrapper<>();
        sysPermissionRoleQueryWrapper.select("PID").eq("RID", sysPermissionRoleItem.getSysRole().getRid());
        Set<String> oldPremiss = new HashSet(this.listObjs(sysPermissionRoleQueryWrapper));
        Set<String> addPremiss = new HashSet<String>();
        Set<String> delPremiss = new HashSet<String>();

        addPremiss.addAll(newPremiss);
        addPremiss.addAll(oldPremiss);
        delPremiss.addAll(newPremiss);
        delPremiss.addAll(oldPremiss);

        addPremiss.removeAll(oldPremiss);
        delPremiss.removeAll(newPremiss);
        SysRole sysRole = sysPermissionRoleItem.getSysRole();

        boolean delFlag = false;
        boolean addFlag = false;

        if (!delPremiss.isEmpty()) {
            List<SysPermissionRole> delPressEn = new ArrayList<>(delPremiss.size());
            QueryWrapper<SysPermissionRole> del = new QueryWrapper<>();
            List<String> result = new ArrayList<>(delPremiss);
//            for (String premission : delPremiss) {
//                SysPermissionRole sysPermissionRole = new SysPermissionRole();
//                sysPermissionRole.setRid(sysRole.getRid());
//                sysPermissionRole.setPid(premission);
//                delPressEn.add(sysPermissionRole);
//                del.eq("RID", sysRole.getRid()).eq("PID", premission);
//                sysPermissionRoleService.remove(del);
//        }
//            sysPermissionRoleService.getBaseMapper().
            delFlag = this.deleteBatchByRidAndPid(sysRole.getRid(), result);
        }
        if (!addPremiss.isEmpty()) {
            List<SysPermissionRole> addPressEn = new ArrayList<>(addPremiss.size());
            for (String premission : addPremiss) {
                SysPermissionRole sysPermissionRole = new SysPermissionRole();
                sysPermissionRole.setRid(sysRole.getRid());
                sysPermissionRole.setPid(premission);
                addPressEn.add(sysPermissionRole);
            }
            addFlag = this.saveBatch(addPressEn);
        }
        return addFlag && delFlag;
    }

    @Override
    public boolean deleteBatchByRidAndPid(String rid, List<String> pids) {
        SqlSession sqlSession = factory.openSession();
        boolean s = sqlSession.getMapper(SysPermissionRoleMapper.class).deleteBatchByRidAndPid(rid, pids);
        return s;
    }


}
